package com.witframework.components.tag;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.PropertiesConstants;
import com.witframework.WitException;
import com.witframework.core.ApplicationProperties;

/**
 * @author wuxinyang
 * Jun 4, 2009 9:37:20 AM
 * 
 */
public class CustomSelectDialogTag extends TagSupport{
	protected String id;//选择组件的ID
	protected String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	protected String properties;//在列表中显示的字段，对于PAGE类型则忽略该属性
	protected String field1;//页面域的id,一般最少需要选择一项数据到目标页面
	protected String property1;//对应的熟悉名称，对于PAGE类型则忽略该属性
	protected String field2;//第二个页面输入域的id
	protected String property2;//上面对应的数据字段名称，对于PAGE类型则忽略该属性
	protected String field3;//第三个页面输入域的id，默认可以有最多三个数据项可以从选择对话框选取。
	protected String property3;//如果需要选取超过三个的数据项，可以设置该属性值为多个字段名,如：email,phone，对于PAGE类型则忽略该属性
	protected String dialogTitle;//对话框的标题
	protected String icon="/webresource/images/ico_btn_search.gif";//图标
	protected String width;//对话框宽度
	protected String height;//对话框高度
	/**
	 * 数据源类型,table:数据库表；class:实体类名(通过对应的service查找数据),默认的数据源类型；
	 * sql:sql 查询语句.如果为sql类型，则filters属性的设置是无效的
	 */
	protected String datatype="class";
	protected String titles;//列表的字段标题,如："name|实体名称,comment|说明,catalog|分类"，对于PAGE类型则忽略该属性
	protected String multiple;//是否允许多选，对于PAGE类型则忽略该属性
	protected String selected;//尚未完全实现，默认的选中项
	
	protected String filterOptions;//可设定的过滤项,目前只支持字符串类型的属性,如:"catalog,name"，对于PAGE类型则忽略该属性
	/**
	 * 数据过滤条件，对于class类型的如：eq:catalog,like:className:A,between:id 
	 * 对于table类型的如:"catalog='system' and name like 'witframework.%'"
	 * 对于PAGE、SQL类型，则忽略该属性
	 */
	private String filters;
	private String show="false";
	private String top="0";
	private String left="0";
	@Override
	public int doStartTag() throws JspException {
		if((datatype==null||datatype.equalsIgnoreCase("table")||datatype.equalsIgnoreCase("sql"))
			&&(properties==null||property1==null)){
			throw new WitException("非page类型的数据源时,必须设置properties和property1属性");
		}
		String dataUrl="/customSelectController"+ApplicationProperties.getProperty(PropertiesConstants.WIT);//数据列表的URL
		if(StringUtils.isNotEmpty(datatype)&&datatype.equalsIgnoreCase("page")){
			dataUrl=data;
		}
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		JspWriter writer = pageContext.getOut();	
		request.getSession().setAttribute("customselect_titles"+id, titles);
		
		if(icon.startsWith("/")&&!icon.startsWith(request.getContextPath())) icon=request.getContextPath()+icon;
		
		if(dataUrl.startsWith("/")&&!dataUrl.startsWith(request.getContextPath())) dataUrl=request.getContextPath()+dataUrl;
		
		
		
		if(StringUtils.isEmpty(dialogTitle)) dialogTitle="对话框";
		
		StringBuffer html=new StringBuffer();
		
		if(show.equalsIgnoreCase("true")||show.equals("1")){
			html.append("<script>showIframe(");
		}else{
			html.append("<a class=\"\" href=# onclick=\"showSelectWin(");
		}
		
		html.append("'"+id+"','"+"<img class=&quot;select_img&quot; src=&quot;"+icon+"&quot; />"
				+dialogTitle+"','"
				+dataUrl+(dataUrl.indexOf("?")>0?"&":"?")+"dialogid="+id);
		
		if(StringUtils.isNotEmpty(datatype)||!datatype.equalsIgnoreCase("page")){
			if(StringUtils.isNotEmpty(data)) html.append("&data="+data);
			if(StringUtils.isNotEmpty(properties)) html.append("&properties="+properties);
			if(StringUtils.isNotEmpty(property1)) html.append("&property1="+property1);
			if(StringUtils.isNotEmpty(property2))	html.append("&property2="+property2);
			if(StringUtils.isNotEmpty(property3))	html.append("&property3="+property3);	
			if(StringUtils.isNotEmpty(selected))	html.append("&selected="+selected);		
			if(StringUtils.isNotEmpty(filterOptions))	html.append("&filterOptions="+filterOptions);	
			if(StringUtils.isNotEmpty(filters)) 	html.append("&filters="+getFilters());		
			if(StringUtils.isNotEmpty(datatype)) html.append("&datatype="+datatype);
		}
		
		if(StringUtils.isNotEmpty(titles))
			html.append("&titles="+titles);
			
//			try {
//				html.append("&titles="+java.net.URLEncoder.encode(titles,"utf-8"));
//				
//			} catch (UnsupportedEncodingException e1) {
//				html.append("&titles="+titles);
//			}		
		if(StringUtils.isNotEmpty(multiple))	html.append("&multiple="+multiple);
			
		html.append("','");
		
		if(StringUtils.isNotEmpty(field1))	html.append("t1="+field1+";");
		if(StringUtils.isNotEmpty(field2))	html.append("t2="+field2+";");
		if(StringUtils.isNotEmpty(field3))	html.append("t3="+field3+";");
		html.append("'");
		
		if(StringUtils.isNotEmpty(width)){
			html.append(",'',"+width);
		}else{
			html.append(",'',''");
		}
		if(StringUtils.isNotEmpty(height)){
			html.append(","+height);
		}else{
			html.append(",''");
		}
		if(StringUtils.isNotEmpty(top)){
			html.append(","+top);
		}else{
			html.append(",'0'");
		}
		if(StringUtils.isNotEmpty(left)){
			html.append(","+left);
		}else{
			html.append(",'0'");
		}
			
		
		if(show.equalsIgnoreCase("true")||show.equals("1")){
			html.append(");</script>");
		}else{
			html.append(");\" >");
			html.append("<img class=\"select_img\" src=\""+icon+"\"/>");
			html.append("</a>");
		}		
		
		try {			
			writer.println(html);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tag.TagSupport#setId(java.lang.String)
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @param data
	 */
	public void setData(String data) {			
		this.data = data;
	}

	/**
	 * @param properties
	 */
	public void setProperties(String properties) {
		this.properties = properties;
	}

	public void setField1(String field1) {
		this.field1 = field1;
	}

	public void setProperty1(String property1) {
		this.property1 = property1;
	}

	public void setField2(String field2) {
		this.field2 = field2;
	}

	public void setProperty2(String property2) {
		this.property2 = property2;
	}

	public void setField3(String field3) {
		this.field3 = field3;
	}

	public void setProperty3(String property3) {
		this.property3 = property3;
	}

	public void setDialogTitle(String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}

	public void setIcon(String icon) {
		this.icon = icon;		
		try {
			this.icon=(String) ExpressionEvaluatorManager.evaluate("icon",icon,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	public void setTitles(String titles) {
		this.titles = titles;
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public void setSelected(String selected) {
		this.selected = selected;		
		try {
			this.selected=(String) ExpressionEvaluatorManager.evaluate("selected",selected,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setFilterOptions(String filterOptions) {
		this.filterOptions = filterOptions;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

	public String getId() {
		return id;
	}

	public String getData() {
		try {
			return java.net.URLEncoder.encode(data,"utf-8");
		} catch (Exception e1) {			
		}
		return data;
	}

	public String getProperties() {
		return properties;
	}

	public String getField1() {
		return field1;
	}

	public String getProperty1() {
		return property1;
	}

	public String getField2() {
		return field2;
	}

	public String getProperty2() {
		return property2;
	}

	public String getField3() {
		return field3;
	}

	public String getProperty3() {
		return property3;
	}

	public String getDialogTitle() {
		return dialogTitle;
	}

	public String getIcon() {
		return icon;
	}

	public String getWidth() {
		return width;
	}

	public String getHeight() {
		return height;
	}

	public String getDatatype() {
		return datatype;
	}

	public String getTitles() {
		return titles;
	}

	public String getMultiple() {
		return multiple;
	}

	public String getSelected() {
		return selected;
	}

	public String getFilterOptions() {
		return filterOptions;
	}

	public String getFilters() {
		try {
			return java.net.URLEncoder.encode(filters,"utf-8");
		} catch (Exception e1) {			
		}
		return filters;
	}

	public String getShow() {
		return show;
	}

	public void setShow(String show) {
		this.show = show;
	}

	public String getTop() {
		return top;
	}

	public void setTop(String top) {
		this.top = top;
	}

	public String getLeft() {
		return left;
	}

	public void setLeft(String left) {
		this.left = left;
	}

}
